package com.sylar.happyreader.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sylar.happyreader.R;
import com.sylar.happyreader.db.Crud;

public class BookPageAdapter extends BaseAdapter {
	private String[] args;
	private Context mContext;
	private Crud dbCrud;
	private String mBookName;
	
	public BookPageAdapter(Context ctx, String[] params,String bookName) {
		this.args = params;
		this.mBookName = bookName;
		this.mContext = ctx;
		if (dbCrud == null)
			dbCrud = new Crud(ctx);
	}

	public int getCount() {
		return args.length;
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		BookPageHolder holder;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.text_row, parent, false);
			holder = new BookPageHolder();
			holder.title = (TextView) convertView.findViewById(R.id.item_title);
			holder.msg = (TextView) convertView.findViewById(R.id.item_content);
			convertView.setTag(holder);
		} else {
			holder = (BookPageHolder) convertView.getTag();
		}
		holder.title.setText(args[position]);
		Boolean isExists = false;
		if (dbCrud != null)
			isExists = dbCrud.checkIsExists(mBookName, args[position].trim());

		holder.msg.setText(isExists ? "已读" : "未读");

		return convertView;
	}

	class BookPageHolder {
		public TextView title;
		public TextView msg;
	}

}
